/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tesoreria.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.general.util.DatosController;
import org.cip.esymons.general.util.InterceptorAuditor;
import org.cip.esymons.tesoreria.be.FormaPago;

/**
 *
 * @author Gilmar Montoya
 */
@Stateless
@DatosController(nombresFacesConfig="formaPagoController")
@Interceptors({InterceptorAuditor.class})
public class FormaPagoFacade implements FormaPagoFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(FormaPago formaPago) {
        em.persist(formaPago);
    }

    public void edit(FormaPago formaPago) {
        em.merge(formaPago);
    }

    public void remove(FormaPago formaPago) {
        em.remove(em.merge(formaPago));
    }

    public FormaPago find(Object id) {
        try {
            return em.find(FormaPago.class, id);
        } catch (Exception e) {
            return null;
        }

    }

    public List<FormaPago> findAll() {
        try {
            return em.createQuery("select object(o) from FormaPago as o").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public FormaPago encontrarCheque() {
        try {
            return (FormaPago)em.createQuery("select object(o) from FormaPago as o Where o.fpaDesc like ?1").setParameter(1,"%Cheque%").getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }

    public List<FormaPago> paginadorFormaPago(int cantidad, int posicion,String desc) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select fp From FormaPago fp ");


            if (desc != null && !desc.isEmpty()) {
                sb.append(" Where fp.fpaDesc like ?1");
            }
            q = em.createQuery(sb.toString());

            if (desc != null && !desc.isEmpty()) {
                q.setParameter(1, "%" + desc + "%");
            }
            q.setFirstResult(posicion);
            q.setMaxResults(cantidad);
            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList();
        }
    }

    public int contarPaginadorFormaPago( String desc) {
       try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select count(fp) From FormaPago fp ");


            if (desc != null && !desc.isEmpty()) {
                sb.append(" Where fp.fpaDesc like ?1");
            }
            q = em.createQuery(sb.toString());

            if (desc != null && !desc.isEmpty()) {
                q.setParameter(1, "%" + desc + "%");
            }
            return new Integer(q.getSingleResult().toString());
        } catch (Exception ex) {
            return 0;
        }
    }


}
